package com.admin.scannercode

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.scanner.smpcapture.ERCodeScannerActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val permissions= arrayOf(Manifest.permission.CAMERA)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        tv_scanner_code.setOnClickListener {
            val rest=PermissionUtil.getPermission(permissions,this)
            if(!rest) {
                val intent = Intent(this@MainActivity, ERCodeScannerActivity::class.java)
                startActivityForResult(intent, 152)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== Activity.RESULT_OK) {
            val r = data?.getStringExtra("result")
            tv_scanner_code.text = r!!
        }
    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(requestCode==PermissionUtil.GET_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                val intent = Intent(this@MainActivity, ERCodeScannerActivity::class.java)
                startActivityForResult(intent, 152)
            } else {
                Toast.makeText(this, "无权限使用", Toast.LENGTH_SHORT).show()
            }
        }
    }

}
